package com.hengxinguotong.zhihuichengjian.adapter;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.bean.Device;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;

/**
 * Created by liux on 2017/3/7.
 */

public class BlueToothDeviceListAdapter extends BaseAdapter {
    private List<BluetoothDevice> boundDevice;

    private List<BluetoothDevice> unboundDevice;
    private Context context;

    public BlueToothDeviceListAdapter(List<BluetoothDevice> boundDevice, Context context) {
        this.context = context;
        this.boundDevice = boundDevice;
        unboundDevice = new ArrayList<BluetoothDevice>();
    }

    public void addBluetoothDevice(BluetoothDevice device) {
        unboundDevice.add(device);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return boundDevice.size() + unboundDevice.size() + 2;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    public List<BluetoothDevice> getUnboundDevice() {
        return unboundDevice;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            TextView title;
            convertView = LayoutInflater.from(context)
                    .inflate(R.layout.bluetooth_list_title, parent, false);
            title = (TextView) convertView.findViewById(R.id.title);
            title.setText("已配对的设备");
            return convertView;
        } else if (position > 0 && position <= boundDevice.size()) {
            TextView name;
            convertView = LayoutInflater.from(context)
                    .inflate(R.layout.bluetooth_list_item, parent, false);
            name = (TextView) convertView.findViewById(R.id.name_tv);
            BluetoothDevice bluetoothDevice = boundDevice.get(position - 1);
            name.setText(bluetoothDevice.getName());
            return convertView;
        } else if (position == boundDevice.size() + 1) {
            TextView title;
            convertView = LayoutInflater.from(context)
                    .inflate(R.layout.bluetooth_list_title, parent, false);
            title = (TextView) convertView.findViewById(R.id.title);
            title.setText("可用设备");
            return convertView;
        } else {
            TextView name;
            convertView = LayoutInflater.from(context)
                    .inflate(R.layout.bluetooth_list_item, parent, false);
            name = (TextView) convertView.findViewById(R.id.name_tv);
            BluetoothDevice bluetoothDevice = unboundDevice.get(position - boundDevice.size() - 2);
            name.setText(bluetoothDevice.getName());
            return convertView;
        }
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return 0;
        } else if (position > 0 && position <= boundDevice.size()) {
            return 1;
        } else if (position == boundDevice.size() + 1) {
            return 0;
        } else {
            return 1;
        }


    }
}
